<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveModel::Validations::Callbacks::ClassMethods</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveModel::Validations::Callbacks::ClassMethods 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../../files/activemodel/lib/active_model/validations/callbacks_rb.html">activemodel/lib/active_model/validations/callbacks.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-after_validation">after_validation</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>B</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-before_validation">before_validation</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-after_validation">
            
              <b>after_validation</b>(*args, &amp;block)
            
            <a href="ClassMethods.html#method-i-after_validation" name="method-i-after_validation" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Defines a callback that will get called right after validation happens.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span><span class="ruby-operator">::</span><span class="ruby-constant">Callbacks</span>

  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">name</span>, :<span class="ruby-identifier">status</span>

  <span class="ruby-identifier">validates_presence_of</span> :<span class="ruby-identifier">name</span>

  <span class="ruby-identifier">after_validation</span> :<span class="ruby-identifier">set_status</span>

  <span class="ruby-identifier">private</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">set_status</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">status</span> = <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">''</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">valid?</span> <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">status</span> <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">valid?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">status</span> <span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-after_validation_source')" id="l_method-i-after_validation_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/22598ee5992fc81d200b3aa99f18248a5259161a/activemodel/lib/active_model/validations/callbacks.rb#L90" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-after_validation_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/callbacks.rb, line 90</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">after_validation</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:prepend</span>] = <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>] = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>])
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>]
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>] = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>])
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>].<span class="ruby-identifier">unshift</span>(<span class="ruby-node">&quot;#{options[:on]}.include? self.validation_context&quot;</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:validation</span>, <span class="ruby-value">:after</span>, *(<span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">options</span>), &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-before_validation">
            
              <b>before_validation</b>(*args, &amp;block)
            
            <a href="ClassMethods.html#method-i-before_validation" name="method-i-before_validation" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Defines a callback that will get called right before validation happens.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span><span class="ruby-operator">::</span><span class="ruby-constant">Callbacks</span>

  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">name</span>

  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">name</span>, <span class="ruby-identifier">maximum</span><span class="ruby-operator">:</span> <span class="ruby-value">6</span>

  <span class="ruby-identifier">before_validation</span> :<span class="ruby-identifier">remove_whitespaces</span>

  <span class="ruby-identifier">private</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">remove_whitespaces</span>
    <span class="ruby-identifier">name</span>.<span class="ruby-identifier">strip!</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'  bob  '</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">valid?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span>   <span class="ruby-comment"># =&gt; &quot;bob&quot;</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-before_validation_source')" id="l_method-i-before_validation_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/22598ee5992fc81d200b3aa99f18248a5259161a/activemodel/lib/active_model/validations/callbacks.rb#L53" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-before_validation_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/callbacks.rb, line 53</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">before_validation</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">last</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>]
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>] = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>])
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>] = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>])
    <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>].<span class="ruby-identifier">unshift</span>(<span class="ruby-node">&quot;#{options[:on]}.include? self.validation_context&quot;</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:validation</span>, <span class="ruby-value">:before</span>, *<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    